// packageI/web-view/h5View.js
import logs from "../../utils/logs";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    if(options.diyUrl){
      if(options.diyUrl == 'success'){
        let successLink = wx.getStorageSync('H5PayCallBackUrl').mini_url;
        this.setData({
          link:successLink
        })
        wx.removeStorageSync('H5PayCallBackUrl')
        return
      }
      if(options.diyUrl == 'failLink'){
        let failLink = wx.getStorageSync('H5PayCallBackUrl').failLink;
        this.setData({
          link:failLink
        })
        return
      }

      this.checkLogin();//验证是否登录
    }else if(options.isEplus){
      this.getAgreementUrl();
    }else{
      this.getData();
    }
  },
  checkLogin() {
    let url = app.getNetAddresss("member.login.check-login");
    app._getNetWork({
      url: url,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          // 新增自定义跳转链接
          // 如果是同站点的自营商城链接需要免登录效果，短链后需要添加参数 &mini = 1
          let _url;
          if(this.options.diyUrl == 'tax_withdraw_bank'){
            _url = "https://m.yunhuotec.com/cloudgainMobile/#/contractedUser"
          }else{
            _url = decodeURIComponent(this.options.diyUrl)
          }
          // let mid = wx.getStorageSync('yz_uid')||0;
          let wx_token = wx.getStorageSync('wx_token');
          logs.info('----------H5View>checkLogin 53-----------',this.options.mini, _url,wx.getStorageSync('yz_uid'))
          if(this.options.mini) {
            _url = `${_url}?mini=1`;
            // if(mid) _url = `${_url}&member_id=${mid}`;
            const separator = _url.includes('?') ? '&' : '?';
            _url = `${_url}${separator}min_token=${wx_token}`;//自营商城免登录
          }
          this.setData({
            link:decodeURIComponent(_url)
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getAgreementUrl() {
    let url = app.getNetAddresss("plugin.eplus-pay.frontend.agreement.agreementUrl");
    app._getNetWork({
      url: url,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            link: res.data.url
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  h5Method(e){
    console.log(e,'bindmessagebindmessagebindmessagebindmessage')
    app.tips(e)
    let arr = e.detail.data;
    let obj = null;
    arr.forEach(element => {
      if(element.mini_url && element.murl){
        obj = element
      }
    });
    if(obj){
      wx.setStorageSync('H5PayCallBackUrl', obj)
      wx.navigateTo({
        url: obj.murl,
      })
      return  
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onShareTimeline(){
    let query = null;
    if(this.options.diyUrl) query = 'diyUrl=' + this.options.diyUrl;
    return{
      query: query
    };
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let path = '/packageI/web-view/h5View';
    if(this.options.diyUrl) path = path + '?diyUrl=' + this.options.diyUrl;
    return {
      path: path
    }
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.min-app.Frontend.Controllers.page.get-news-link");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          this.setData({
            link: res.data.url
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
    });
  }
});